package com.teragadgets.android.comic;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class NavigationControls extends LinearLayout implements
		AnimationListener {

	private final ImageButton leftButton;
	private final ImageButton rightButton;
	private final LinearLayout leftButtonWrapper;
	private final LinearLayout rightButtonWrapper;

	public NavigationControls(Context context) {
		this(context, null);
	}

	public NavigationControls(Context context, AttributeSet attrs) {
		super(context, attrs);
		setFocusable(false);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.nav_control, this, // we are the parent
				true);

		leftButton = (ImageButton) findViewById(R.id.left_arrow);
		rightButton = (ImageButton) findViewById(R.id.right_arrow);
		leftButtonWrapper = (LinearLayout) findViewById(R.id.left_arrow_wrapper);
		rightButtonWrapper = (LinearLayout) findViewById(R.id.right_arrow_wrapper);
		setVisibility(View.INVISIBLE);
	}

	public void quickShow() {
		this.clearAnimation();
		this.showing = true;
		this.setVisibility(View.VISIBLE);
		hide();
	}

	public void show() {
		if (this.showing) {
			this.clearAnimation();
			return;
		}
		this.setVisibility(View.VISIBLE);
		fade(View.VISIBLE, 0.0f, 1.0f);
	}

	public void hide() {
		if (!this.showing) {
			this.clearAnimation();
			this.setVisibility(View.INVISIBLE);
			return;
		}
		fade(View.INVISIBLE, 1.0f, 0.0f);
	}

	private void fade(int visibility, float startAlpha, float endAlpha) {
		this.clearAnimation();
		AlphaAnimation anim = new AlphaAnimation(startAlpha, endAlpha);
		anim.setAnimationListener(this);
		this.visibility = visibility;
		anim.setDuration(500);
		anim.setStartOffset(1000);
		this.startAnimation(anim);

	}

	private int visibility;
	private boolean showing;

	public void setIsLeftArrowEnabled(boolean isEnabled) {
		leftButton.setEnabled(isEnabled);
	}

	public void setIsRightArrowEnabled(boolean isEnabled) {
		rightButton.setEnabled(isEnabled);
	}

	@Override
	public boolean hasFocus() {
		return leftButton.hasFocus() || rightButton.hasFocus();
	}

	public void setOnLeftArrowClickListener(OnClickListener listener) {
		leftButtonWrapper.setOnClickListener(listener);
	}

	public void setOnRightArrowClickListener(OnClickListener listener) {
		rightButtonWrapper.setOnClickListener(listener);
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		this.setVisibility(visibility);
		this.showing = (visibility == View.VISIBLE);
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
	}

	@Override
	public void onAnimationStart(Animation animation) {
	}

	@Override
	public void clearAnimation() {
		leftButton.clearAnimation();
		rightButton.clearAnimation();
	}

	@Override
	public void setVisibility(int visibility) {
		leftButton.setVisibility(visibility);
		rightButton.setVisibility(visibility);
	}

	@Override
	public void startAnimation(Animation animation) {
		leftButton.startAnimation(animation);
		rightButton.startAnimation(animation);
	}

}